/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pass_client;

import javax.swing.JOptionPane;
import pasremote.PASRemote;

/**
 *
 * @author Administrator
 */
public class RegisterFrame extends javax.swing.JFrame {
    private final PASRemote bean;

    /**
     * Creates new form RegisterFrame
     */

    RegisterFrame(PASRemote bean) {
        this.bean = bean;
        initComponents();
        initUserComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtID = new javax.swing.JTextField();
        txtPassword = new javax.swing.JTextField();
        radioStudent = new javax.swing.JRadioButton();
        radioAssistant = new javax.swing.JRadioButton();
        txtAsstPw = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        btnRegister = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Register");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setText("ID");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(68, 13, -1, -1));

        jLabel2.setText("PASSWORD");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(12, 40, -1, -1));

        jLabel3.setText("TYPE");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(47, 65, -1, -1));
        getContentPane().add(txtID, new org.netbeans.lib.awtextra.AbsoluteConstraints(91, 10, 144, -1));
        getContentPane().add(txtPassword, new org.netbeans.lib.awtextra.AbsoluteConstraints(91, 37, 144, -1));

        radioStudent.setText("Student");
        radioStudent.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioStudentActionPerformed(evt);
            }
        });
        getContentPane().add(radioStudent, new org.netbeans.lib.awtextra.AbsoluteConstraints(91, 61, -1, -1));

        radioAssistant.setText("Assistant");
        radioAssistant.setToolTipText("");
        radioAssistant.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioAssistantActionPerformed(evt);
            }
        });
        getContentPane().add(radioAssistant, new org.netbeans.lib.awtextra.AbsoluteConstraints(158, 61, -1, -1));

        txtAsstPw.setEnabled(false);
        getContentPane().add(txtAsstPw, new org.netbeans.lib.awtextra.AbsoluteConstraints(93, 90, 142, -1));

        jLabel4.setText("ASST_PW");
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(25, 93, -1, -1));

        btnRegister.setText("Register");
        btnRegister.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRegisterActionPerformed(evt);
            }
        });
        getContentPane().add(btnRegister, new org.netbeans.lib.awtextra.AbsoluteConstraints(76, 121, -1, -1));

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });
        getContentPane().add(btnCancel, new org.netbeans.lib.awtextra.AbsoluteConstraints(162, 121, -1, -1));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void radioAssistantActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioAssistantActionPerformed
        if(radioAssistant.isSelected()){
            txtAsstPw.setEnabled(true);
            radioStudent.setSelected(false);
        }
    }//GEN-LAST:event_radioAssistantActionPerformed

    private void btnRegisterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRegisterActionPerformed
        if(radioStudent.isSelected()){
            if(bean.register(txtID.getText(), txtPassword.getText(), "Student", null)){
                JOptionPane.showMessageDialog(this, "Register Success!");
                this.dispose();
            } else {
                JOptionPane.showMessageDialog(this, "Register Failed!");
            }
        } else if(radioAssistant.isSelected()){
            if(bean.register(txtID.getText(), txtPassword.getText(), "Assistant", txtAsstPw.getText())){
                JOptionPane.showMessageDialog(this, "Register Success!");
                this.dispose();
            }else {
                JOptionPane.showMessageDialog(this, "Register Failed!");
            }
        }
    }//GEN-LAST:event_btnRegisterActionPerformed

    private void radioStudentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioStudentActionPerformed
        if(radioStudent.isSelected()){
            txtAsstPw.setEnabled(false);
            radioAssistant.setSelected(false);
        }
    }//GEN-LAST:event_radioStudentActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        LoginFrame loginFrame = new LoginFrame(bean);
    }//GEN-LAST:event_formWindowClosed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCancelActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnRegister;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JRadioButton radioAssistant;
    private javax.swing.JRadioButton radioStudent;
    private javax.swing.JTextField txtAsstPw;
    private javax.swing.JTextField txtID;
    private javax.swing.JTextField txtPassword;
    // End of variables declaration//GEN-END:variables
    private void initUserComponents() {
        setSize(260, 200);
        setLocationByPlatform(true);
        setVisible(true);        
    }


}
